<?php
/**
 * Created by JetBrains PhpStorm.
 * User: oliverson1989
 * Date: 1/12/13
 * Time: 12:21 PM
 * To change this template use File | Settings | File Templates.
 */
$config=parse_ini_file("/config/database.ini");
$host=$config["host"];
$dbname=$config["database"];
$id=$config["user_name"];
$pass=$config["password"];
$str_page="";
if (!$cn=mysql_connect($host, $id, $pass)) {
    echo 'Could not connect to mysql';
    exit;
}
if(isset($_GET["page"]))
{
    $curent=$_GET["page"];
    $str_page="&page=".$_GET["page"];
}
else
{
    $curent=0;
}
$perpage=10;
$sql="SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '".$dbname."' ORDER BY TABLE_NAME ASC;";
$result = mysql_query($sql,$cn) or die(connect_fail());
if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
else
{
    $paging=new Paging02();
    $paging->total_rows=mysql_num_rows($result);
    $paging->curent=$curent;
    $paging->perpage=$perpage;
    $paging->url="GeneralCode.php?action=general_code.php";
    $paging_str= $paging->show();
    $curent=$paging->curent;
}
$sql="SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '".$dbname."' ORDER BY TABLE_NAME ASC LIMIT $curent,$perpage";
$result = mysql_query($sql) or die(connect_fail());
function connect_fail()
{
    echo "database not exits.";
    exit;
}
if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
?>
<table class="table">
    <tr>
        <th colspan="3">
            General Code
        </th>
    </tr>
    <tr>
        <th>
            Table Name
        </th>
        <th>
            General Model
        </th>
        <th>
            General Form
        </th>
    </tr>
<?php
while ($row = mysql_fetch_row($result)) {
    ?>
    <tr>
        <td style="padding: 5 5 5 5;">
            <?php echo "{$row[0]}"; ?>
        </td>
        <td>
        <a style="color: blue;" href="GeneralCode.php?action=general_code.php&general=model&table_name=<?php echo "{$row[0]}".$str_page; ?>">Arising</a>
        </td>
        <td>
            <a style="color: blue;" href="GeneralCode.php?action=general_code.php&general=form&table_name=<?php echo "{$row[0]}.$str_page"; ?>">Arising</a>
        </td>
    </tr>
    <?php
}
mysql_free_result($result);
?>
    <tr>
        <td colspan="3">
            <?php echo $paging_str; ?>
        </td>
    </tr>
</table>
<?php

function write_File($filename,$content) {
    $f=fopen($filename,"w") or die();
    fwrite($f,$content);
    fclose($f);
}
function Generate_Model_Table($table,$dbname,$cn)
{
    // write file root model
    $str_write_file_php="";
    $f = fopen (ROOT_PATH."/GeneralCode/TemplateCode/template_generate_model_root.php", "r");
    while ($line= fgets ($f)) {
        if ($line===FALSE) print ("FALSE\n");
        else $str_write_file_php.=$line;
    }
    fclose ($f);
    $str_write_file_php=str_replace("{table}",$table,$str_write_file_php);
    $str_write_file_php=str_replace("{date}",date("Y-m-d H:i:s"),$str_write_file_php);
    $str_write_file_php=str_replace("/**{class}*/",$table,$str_write_file_php);
    $attributes="";
    mysql_select_db($dbname, $cn);
    $result = mysql_query("SHOW COLUMNS FROM ".$table,$cn);
    if (!$result) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    $where="";
    $value_update="";
    $attributes_insert="";
    $attributes_value="";
    $load_value="";
    if (mysql_num_rows($result) > 0)
    {
        while ($row = mysql_fetch_assoc($result)) {
            if($row["Key"]=="PRI")
            {
                if($row["Extra"]!="auto_increment")
                {
                    if(empty($attributes_insert))
                    {
                        $attributes_insert=$row["Field"];
                        $attributes_value="'\r\n\t\t\t\t.$"."this->".$row["Field"];
                    }
                    else
                    {
                        $attributes_insert.="'"."\r\n\t\t\t\t.', ".$row["Field"];
                        $attributes_value.=".'".'", '.'"'."'\r\n\t\t\t\t.$"."this->".$row["Field"];
                    }
                }
                if(empty($where))
                {
                    $where="WHERE ".$row["Field"].' = "'."'.$"."this->".$row["Field"];
                }
                else
                {
                    $where.=".'".'" AND '."'"."\r\n\t\t\t\t.'".$row["Field"].' = "'."'.$"."this->".$row["Field"];
                }
            }
            else
            {
                if(empty($value_update))
                {
                    $value_update=$row["Field"].' = "'."'.$"."this->".$row["Field"];
                }
                else
                {
                    $value_update.=".'".'", '."'"."\r\n\t\t\t\t.'".$row["Field"].' = "'."'.$"."this->".$row["Field"];
                }
                if(empty($attributes_insert))
                {
                    $attributes_insert=$row["Field"];
                    $attributes_value="'\r\n\t\t\t\t.$"."this->".$row["Field"];
                }
                else
                {
                    $attributes_insert.="'"."\r\n\t\t\t\t.', ".$row["Field"];
                    $attributes_value.=".'".'", '.'"'."'\r\n\t\t\t\t.$"."this->".$row["Field"];
                }
            }
            if(empty($load_value))
            {
                $load_value.="$"."this->".$row["Field"]."=$"."value['".$row["Field"]."'];\r\n";
            }
            else
            {
                $load_value.="\t\t\t$"."this->".$row["Field"]."=$"."value['".$row["Field"]."'];\r\n";
            }
            $attributes.="\tprotected $".$row["Field"].";\r\n";
        }
        $value_update.=".'".'" ';
        $where.=".'".'" LIMIT 1;';
        $attributes_value.=".'";
    }
    $str_write_file_php=str_replace("/**{attributes}*/",$attributes,$str_write_file_php);
    $str_write_file_php=str_replace("{value_update}",$value_update,$str_write_file_php);
    $str_write_file_php=str_replace("{where}",$where,$str_write_file_php);
    $str_write_file_php=str_replace("{attributes_insert}",$attributes_insert,$str_write_file_php);
    $str_write_file_php=str_replace("{attributes_value}",$attributes_value,$str_write_file_php);
    $str_write_file_php=str_replace("/**{load_value}*/",$load_value,$str_write_file_php);
    write_File(ROOT_PATH."/library/Model/Root/root_model_".$table.".php",$str_write_file_php);
    // write file model
    if(!file_exists(ROOT_PATH."/library/Model/model_".$table.".php"))
    {
        $str_write_file_php="";
        $f = fopen (ROOT_PATH."/GeneralCode/TemplateCode/template_generate_model.php", "r");
        while ($line= fgets ($f)) {
            if ($line===FALSE) print ("FALSE\n");
            else $str_write_file_php.=$line;
        }
        fclose ($f);
        $str_write_file_php=str_replace("{table}",$table,$str_write_file_php);
        $str_write_file_php=str_replace("{date}",date("Y-m-d H:i:s"),$str_write_file_php);
        $str_write_file_php=str_replace("/**{class}*/",$table,$str_write_file_php);
        write_File(ROOT_PATH."/library/Model/model_".$table.".php",$str_write_file_php);
    }
}
function check_folder($path)
{
    if(!is_dir($path))
    {
        mkdir($path);
    }
}
if(isset($_GET["table_name"]) && isset($_GET["general"]))
{
    check_folder(ROOT_PATH."/library");
    check_folder(ROOT_PATH."/library/Model");
    check_folder(ROOT_PATH."/library/Model/Root");
    if($_GET["general"]=="model")
    {
        Generate_Model_Table($_GET["table_name"],$dbname,$cn);
    }
}
mysql_close($cn);
?>